package com.atguigu.day17.collection02;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

/*
Collection: 集合的顶级接口
    -- List
       -- ArrayList


 */

public class CollectionTest {

    @Test
    public void test06(){
        Collection<String> str = new ArrayList<>();
        str.add("abc");
        str.add("def");
        str.add("ghi");
        String[] sArr = str.toArray(new String[]{});
        for (String s : sArr) {
            System.out.println(s + ", " + s.toUpperCase());
        }
    }


    @Test
    public void test05(){
        Collection<Integer> i = new ArrayList<>();
        i.add(1);
        i.add(2);
        i.add(3);
        i.add(4);
        i.add(5);
        i.add(6);
        Integer[] arr = i.toArray(new Integer[]{});
        for (Integer integer : arr) {
            System.out.println(integer);
        }

    }

    @Test
    public void test04() {
        Collection<String> c = new ArrayList<>();
        c.add("bob");
        c.add("jim");
        c.add("marry");
        //将集合转为数组 指定数组的类型
        String[] array = c.toArray(new String[]{});

        for (String s : array) {
            System.out.println("s = " + s+","+s.toUpperCase());
        }

        //将集合转为数组
      /*  Object[] array = c.toArray();
        for (Object o : array) {
            String s = (String) o;
            System.out.println(s.toUpperCase()+","+s);
        }*/


    }

    @Test
    public void test03() {
        Collection<String> c = new ArrayList<>();
        c.add("张三");
        c.add("李四");
        c.add("王五");
        c.add("刘亦菲");

        Collection<String> c2 = new ArrayList<>();
        c2.add("李四");
        c2.add("刘亦菲");
        c2.add("安琪拉");
        //交集
        c.retainAll(c2);

        System.out.println("c = " + c);//[李四, 刘亦菲]

        //获取集合内元素的数量
        int size = c.size();
        System.out.println("size = " + size);//2

        //清空集合
        c.clear();
        System.out.println("c = " + c);
        System.out.println("c.isEmpty() = " + c.isEmpty());


    }

    @Test
    public void test02() {
        Collection<String> c = new ArrayList<>();
        //增加
        c.add("张三");
        c.add("李四");
        c.add("王五");
        c.add("赵六");
        System.out.println("c = " + c);// [张三, 李四, 王五, 赵六]

        Collection<String> c2 = new ArrayList<>();
        c2.add("杨幂");
        c2.add("赵丽颖");
        c2.add("刘亦菲");
        //添加一个集合
        c.addAll(c2);
        System.out.println("c = " + c);//[张三, 李四, 王五, 赵六, 杨幂, 赵丽颖, 刘亦菲]

        //删除
        c.remove("王五");
        System.out.println("c = " + c);//[张三, 李四, 赵六, 杨幂, 赵丽颖, 刘亦菲]

        //删除集合
        c.removeAll(c2);
        System.out.println("c = " + c);// [张三, 李四, 赵六]

        //查询
        boolean contains = c.contains("赵六9");
        System.out.println("contains = " + contains);
    }

    @Test
    public void test01() {
        //多态
        //todo 泛型:规定输入数据的类型
        Collection<Integer> c = new ArrayList();
       /* c.add("张三");
        c.add("李四");
        c.add(3.14);*/
        c.add(300);
        c.add(100);

        System.out.println("c = " + c);
    }
}
